package com.example.demo.controller;


import com.example.comon.domain.R;
import com.example.demo.domain.Bo.RoleMenuListBo;
import com.example.demo.domain.Menu;
import com.example.demo.domain.RoleMenu;
import com.example.demo.domain.Vo.RoleMenuListVo;
import com.example.demo.service.IRoleMenuService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/roleMenu")
@Api(value = "roleMenu",tags = "角色菜单控制器")
public class RoleMenuController {

    @Autowired
    private IRoleMenuService roleMenuService;


    @GetMapping("/getRoleMenu")
    @ApiOperation("获取角色菜单")
    public R<List<RoleMenuListVo>> getRoleMenuList(RoleMenuListBo bo) {
        RoleMenu roleMenu = new RoleMenu();
        Long roleId = bo.getRoleId();
        Long menuId = bo.getMenuId();
        roleMenu.setRoleId(bo.getRoleId());
        List<RoleMenuListVo> list = (List<RoleMenuListVo>) roleMenuService.selectByRoleIdAndMenuId(roleId, menuId);
        return R.success(list);
    }

    @PostMapping("/addRoleMenu")
    @ApiOperation("增加角色菜单")
    public R addRoleMenu(@RequestBody RoleMenuListBo bo) {
        RoleMenu roleMenuRes = roleMenuService.selectByRoleIdAndMenuId(bo.getMenuId(),bo.getRoleId());
        if(roleMenuRes !=null){
            return R.error("该用户已授权该角色，请勿重复授权");
        }
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(bo.getRoleId());
        roleMenu.setMenuId(bo.getMenuId());
        roleMenu.setCreateBy(1111L);
        roleMenu.setUpdateBy(2222L);
        roleMenu.setCreateTime(new Date());
        roleMenu.setUpdateTime(new Date());
        roleMenuService.insertRoleMenu(roleMenu);
        return R.success("授权成功");
    }


    /**
     * 查询角色的菜单（角色有哪些菜单）
     * @param roleId
     * @return
     */
    @PostMapping("/getMenuByRoleId")
    @ApiOperation("查询角色有哪些菜单")
    public R getMenuByRoleId(Long roleId){

        List<Menu>  list= roleMenuService.getMenuByRoleId(roleId);
        return R.success(list);
    }

}
